﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Timers;
using ayxjDllClass;
using System.Configuration;
using System.IO;
using System.Net;

namespace _6.jkxt
{
    public class Rotation
    {
        //_DataPath = GetAppConfig("DataPath");
        //UpdateAppConfig("AzOffset", _AzOffset.ToString());
        private string GetAppConfig(string strKey)
        {
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            foreach (string key in config.AppSettings.Settings.AllKeys)
            {
                if (key == strKey)
                {
                    return config.AppSettings.Settings[strKey].Value.ToString();
                }
            }
            return null;
        }

        private void UpdateAppConfig(string newKey, string newValue)
        {
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            bool exist = false;
            foreach (string key in config.AppSettings.Settings.AllKeys)
            {
                if (key == newKey)
                {
                    exist = true;
                }
            }
            if (exist)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            config.AppSettings.Settings.Add(newKey, newValue);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        public void getWebData()
        {
            string url = Main.csName.SatelliteDataURL;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //声明一个HttpWebRequest请求 
            request.Timeout = 30000;
            //设置连接超时时间 
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = Encoding.GetEncoding("UTF-8");
            StreamReader SR = new StreamReader(streamReceive, encoding);
            string satelliteName = "";
            string firstLine = "";
            string secondLine = "";
            while (!SR.EndOfStream)
            {
               
                string str = SR.ReadLine();
                if (str.Length > 1 && str[0] != '1' && str[1] != '2')
                {
                   satelliteName = str;
                    if (!SR.EndOfStream)
                    {
                        str = SR.ReadLine();
                        if (str.Length > 0 && str[0] == '1')
                        {
                           firstLine = str;
                            if (!SR.EndOfStream)
                            {
                                str = SR.ReadLine();
                                if (str.Length > 0 && str[0] == '2')
                                {
                                    secondLine = str;
                                    ///写一个卫星数据
                                    string fileFullPath = Environment.CurrentDirectory + @"\text\\SatelliteData" + @"\" + satelliteName + ".txt";
                                    using (FileStream file = File.Open(fileFullPath, FileMode.OpenOrCreate))
                                    {
                                        using (StreamWriter SW = new StreamWriter(file))
                                        {
                                            SW.WriteLine(satelliteName);
                                            SW.WriteLine(firstLine);
                                            SW.WriteLine(secondLine);
                                            SW.Flush();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
            SR.Close();
        }

        private static Rotation RotationControll = null;
        public static Rotation GetInstancce()
        {
            if (RotationControll == null) { RotationControll = new Rotation(); }
            return RotationControll;
        }
        TurnDirect _TDNowAz = TurnDirect.StopAz;
        TurnDirect _TDNowEl = TurnDirect.StopEl;
        public bool IsPauseAzTask = false; //是否暂停方位任务
        public bool IsPauseElTask = false; //是否暂停俯仰任务
        #region 转台控制
        /// <summary>
        /// 旋转速度限值
        /// </summary>
        double _DAzSpeedLimitMin = 0.01;
        double _DAzSpeedLiitMax = 100;
        double _DElSpeedLimitMin = 0.01;
        double _DElSpeedLimitMax = 100;
        double CheckSpeed(Direct dt, double d)//// 检查速度限值
        {
            double speed = d;
            switch (dt)
            {
                case Direct.AZ:
                    if (d < _DAzSpeedLimitMin)
                    {
                        speed = _DAzSpeedLimitMin;
                    }
                    else if (d > _DAzSpeedLiitMax)
                    {
                        speed = _DAzSpeedLiitMax;
                    }
                    break;

                case Direct.EL:
                    if (d < _DElSpeedLimitMin)
                    {
                        speed = _DElSpeedLimitMin;
                    }
                    else if (d > _DElSpeedLimitMax)
                    {
                        speed = _DElSpeedLimitMax;
                    }
                    break;

            }
            return speed;
        }
        /// <summary>
        /// 启动旋转
        /// </summary>
        /// <param name="TD"></param>
        /// <param name="Speed"></param>
        public void Turn(TurnDirect TD, double Speed)
        {
            switch (TD)
            {
                case TurnDirect.Down:
                    if (AxisControl.getPosition((short)Direct.EL - 1) > Convert.ToDouble(Main.csName.pl[22, 1]))
                    {
                        Speed = CheckSpeed(Direct.EL, Speed);
                        AxisControl.setSpeed(1, Speed);
                        AxisControl.setMove(1, 2);
                        _TDNowEl = TD;
                    }
                    break;
                case TurnDirect.Left:
                    if (AxisControl.getPosition((short)Direct.AZ - 1) > Convert.ToDouble(Main.csName.pl[20, 1]))
                    {
                        Speed = CheckSpeed(Direct.AZ, Speed);
                        AxisControl.setSpeed(0, Speed);
                        AxisControl.setMove(0, 2);
                        _TDNowAz = TD;
                    }
                    break;
                case TurnDirect.Right:
                    if (AxisControl.getPosition((short)Direct.AZ - 1) < Convert.ToDouble(Main.csName.pl[19, 1]))
                    {
                        Speed = CheckSpeed(Direct.AZ, Speed);
                        AxisControl.setSpeed(0, Speed);
                        AxisControl.setMove(0, 1);
                        _TDNowAz = TD;
                    }
                    break;
                case TurnDirect.Up:
                    if (AxisControl.getPosition((short)Direct.EL - 1) < Convert.ToDouble(Main.csName.pl[21, 1]))
                    {
                        Speed = CheckSpeed(Direct.EL, Speed);
                        AxisControl.setSpeed(1, Speed);
                        AxisControl.setMove(1, 1);
                        _TDNowEl = TD;
                    }
                    break;
                case TurnDirect.StopAz:
                    AxisControl.setMove(0, 0);
                    _TDNowAz = TD;
                    break;
                case TurnDirect.StopEl:
                    AxisControl.setMove(1, 0);
                    _TDNowEl = TD;
                    break;
            }
        }
        #endregion
    }

    /// <summary>
    /// 转到指定角度的方向
    /// </summary>
    public enum Direct
    {
        /// <summary>
        /// 1
        /// </summary>
        AZ = 0x01,
        /// <summary>
        /// 2
        /// </summary>
        EL = 0x02,
        /// <summary>
        /// 3
        /// </summary>
        P=0x03,
    }
    /// <summary>
    /// 旋转方向
    /// </summary>
    public enum TurnDirect
    {
        Left = 0x01,
        Right = 0x02,
        Up = 0x03,
        Down = 0x04,
        StopAz = 0x00,
        StopEl = 0x10,
        TurnToAngle = 0x05
    }
   
}
